/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package fr.miage.shopping.presentation;

import fr.miage.shopping.dao.traitements.CategorieDao;
import fr.miage.shopping.metier.entite.Categorie;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 *
 * @author MACAJ
 */
public class CreationCategorie extends JFrame implements ActionListener {
    private JLabel lNomCategorie=new JLabel("Nom");
    private JLabel lIdCategoriePere=new JLabel("Categorie père");
    private JTextField tNomCategorie=new JTextField(30);
    private JComboBox categories;
    private JButton bAnnuler=new JButton("Annuler");
    private JButton bOK=new JButton("OK");
    public CreationCategorie(){
        super("Créer catégorie");
        initFrame();
    }
    /**
     * Initialisation de la frame
     */
    public void initFrame(){
        Container pane = this.getContentPane();

        JPanel panelNorth=new JPanel();
        panelNorth.setLayout(new FlowLayout());
        panelNorth.add(lNomCategorie);
        panelNorth.add(tNomCategorie);
        pane.add(panelNorth,BorderLayout.NORTH);

        JPanel panelCenter=new JPanel();
        panelCenter.setLayout(new FlowLayout());
        panelCenter.add(lIdCategoriePere);
        categories=new JComboBox(getCategories());
        panelCenter.add(categories);
        pane.add(panelCenter,BorderLayout.CENTER);

//        panelCenter.add(tIdCategoriePere);

        JPanel panelSouth=new JPanel();
        panelSouth.setLayout(new FlowLayout());
        this.bOK.addActionListener(this);
        this.bOK.setActionCommand("ok");
        panelSouth.add(bOK);
        this.bAnnuler.addActionListener(this);
        this.bAnnuler.setActionCommand("annuler");
        panelSouth.add(bAnnuler);
        pane.add(panelSouth,BorderLayout.SOUTH);
        this.pack();
        this.setVisible(true);
    }
    /**
     * Récupère un vecteur de catégories
     * @return
     */
    public Vector<String> getCategories(){
        List<Categorie> list= CategorieDao.selectAllCategorieTrie();//CategorieDao.selectAllCategorie();
        Vector<String> categoriesVector=new Vector<String>();
        categoriesVector.add("");
        for(int i = 0;i<list.size();i++){
            categoriesVector.add(list.get(i).getNomCate());
        }
        return categoriesVector;
    }

    /**
     * Action
     * @param e
     */
    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equalsIgnoreCase("annuler")) {
            this.setVisible(false);
            //procedure de annuler
        }else{
            String nomCategorie=tNomCategorie.getText();
            if(nomCategorie.trim().length()==0||nomCategorie==null){
                JOptionPane.showMessageDialog(this,"Tous les champs doivent etre obligatoirement rempli ! ");
            }else{
                Categorie categorie=new Categorie();
                categorie.setNomCate(nomCategorie);
                if(CategorieDao.selectCategorieByNom(categorie)==null){
                    Categorie cateSelect=new Categorie();
                    String nomPere=(String)categories.getSelectedItem();
                    cateSelect.setNomCate(nomPere);
                    Categorie categoriePere=CategorieDao.selectCategorieByNom(cateSelect);
                    categorie.setCategorie(categoriePere);
//                    categorie.set(categoriePere.getIdCate());
                    CategorieDao.insertCategorie(categorie);
                 }else{
                    JOptionPane.showMessageDialog(this, "La categorie existe deja ");
                    this.tNomCategorie.setText("");
                 }

                this.setVisible(false);
        }
    }

}

}
